package xyz.chaobei.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.chaobei.entity.OrgPermissionEntity;
import xyz.chaobei.entity.OrgUserEntity;
import xyz.chaobei.execption.NullDataException;
import xyz.chaobei.mapper.OrgPermissionMapper;
import xyz.chaobei.mapper.OrgRoleMapper;
import xyz.chaobei.service.OrgPermissionService;

import java.util.LinkedList;
import java.util.List;

/**
 * service impl
 *
 * @author mrc
 */
@Service
@Slf4j
public class OrgPermissionServiceImpl extends ServiceImpl<OrgPermissionMapper, OrgPermissionEntity> implements OrgPermissionService {

    @Autowired
    private OrgRoleMapper orgRoleMapper;

    @Override
    public List<OrgPermissionEntity> findByRoleId(Integer id) {
        return baseMapper.findByRoleId(id);
    }

    @Override
    public List<Integer> findIdsByRoleId(Integer id) {
        List<Integer> roles = new LinkedList<>();
        roles.add(id);

        List<Integer> permission = baseMapper.findPermissionByRoles(roles);
        return permission;
    }

    @Override
    public List<OrgPermissionEntity> findUserMenu() {

        Subject currentUser = SecurityUtils.getSubject();
        OrgUserEntity orgUserEntity = (OrgUserEntity) currentUser.getPrincipal();

        List<Integer> roles = orgRoleMapper.findRoleIdsByUserId(orgUserEntity.getId());
        List<Integer> permission = baseMapper.findPermissionByRoles(roles);

        List<OrgPermissionEntity> result = this.findMenuByParent(0, permission);
        return result;
    }

    @Override
    public List<OrgPermissionEntity> findTree() {
        List<OrgPermissionEntity> result = findTreeByParent(0);
        return result;
    }

    @Override
    public List<OrgPermissionEntity> findAllPermission() {

        List<OrgPermissionEntity> result = new LinkedList<>();

        this.findAllPermissionByParent(0, result);

        return result;
    }

    @Override
    public boolean updateIndex(Integer index, Integer id) {

        OrgPermissionEntity entity = baseMapper.findById(id);
        if (null == entity) {
            throw new NullDataException();
        }
        int num = baseMapper.updateIndex(index, id);
        return true;
    }

    /**
     *
     * @param parent
     * @param result
     */
    public void findAllPermissionByParent(Integer parent, List<OrgPermissionEntity> result) {


        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id_",parent);
        wrapper.orderByAsc("index_");

        List<OrgPermissionEntity> list = baseMapper.selectList(wrapper);

        for (OrgPermissionEntity temp : list) {
            result.add(temp);
            findAllPermissionByParent(temp.getId(), result);
        }
    }


    public List<OrgPermissionEntity> findTreeByParent(Integer parent) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id_",parent);
        wrapper.orderByAsc("index_");
        List<OrgPermissionEntity> list = baseMapper.selectList(wrapper);

        for (OrgPermissionEntity temp : list) {
            List<OrgPermissionEntity> children = findTreeByParent(temp.getId());
            temp.setChildren(children);
        }
        return list;
    }

    public List<OrgPermissionEntity> findMenuByParent(Integer parent, List<Integer> permission) {
        List<OrgPermissionEntity> result = baseMapper.findMenuByParent(parent, permission);
        for (OrgPermissionEntity temp : result) {
            List<OrgPermissionEntity> children = this.findMenuByParent(temp.getId(), permission);
            temp.setChildren(children);
        }
        return result;
    }
}